package com.shujia.day12;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;

public class Demo10BuffedRead {
    public static void main(String[] args) throws IOException {
        /*
            TODO 缓冲流
                    BufferedReader
                        readLine 可以获取一行数据 如果获取到则为字符串，获取不到，则返回null

         */
        String path = "data/students.txt";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
        // 获取的是一个整行数据
//        String line1 = bufferedReader.readLine();
//        System.out.println(line1);
//        String line2 = bufferedReader.readLine();
//        System.out.println(line2);
//        String line3 = bufferedReader.readLine();
//        System.out.println(line3);
//        for (int i = 0; i < 1001; i++) {
//            String line = bufferedReader.readLine();
//            System.out.println(line);
//        }
        String line = "";
        ArrayList<Student> list = new ArrayList<>();
        while ((line = bufferedReader.readLine())!= null){
            // TODO 当读取到的一行数据其不为null的情况下 可以对该行数据进行处理
            String[] columns = line.split(",");
            list.add(new Student(columns[0],columns[1],Integer.valueOf(columns[2]),columns[3],columns[4]));
        }

        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student thisStu, Student otherStu) {
                int clazzRes = thisStu.clazz.compareTo(otherStu.clazz);
                return clazzRes == 0 ? thisStu.id.compareTo(otherStu.id):clazzRes;
            }
        });

        for (Student student : list) {
            System.out.println(student);
        }


    }
}

class Student implements Serializable {
    String id;
    String name;
    int age;
    String gender;
    String clazz;

    public Student(String id, String name, int age, String gender, String clazz) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.clazz = clazz;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", clazz='" + clazz + '\'' +
                '}';
    }
}
